/* Replication Code for "Charity Begins at Home" */

* Change to your path
global repfolder "H:\Research Project 3\Dong-Sibo\Aid_Trade\replication\code\replication package [public]\"

* control variables
global ctrl0 "GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF" // country controls
global ctrl1 "LNTA LEV ROA" // firm controls
global ctrl2 "LNTA LEV ROA GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF" // country + firm controls

cd "$repfolder/data"

*=============================================================================== 
* 1. Tables and Figures in the Main Text
*=============================================================================== 

* main text log file
log using "$repfolder\result\log_file_main.log"

*** T1
** T1A
use "Sample_ctry_year.dta",clear

reghdfe ln_trade tr2 ,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1A.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_trade tr2 $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1A.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_export tr2,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1A.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2 $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1A.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_import tr2,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1A.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_import tr2 $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1A.xls", append dec(3) adjr2  e(r2_within)

** T1B_1_2
use "Sample_ctry_ind_year.dta",clear

reghdfe ln_export tr2 $ctrl0,  abs(ctry_id sitc3 year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1B_1_2.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2 $ctrl0,  abs(ctry_id ind_yr) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1B_1_2.xls", append dec(3) adjr2  e(r2_within)

** T1B_3_4
use "Sample_firm_ctry_year.dta",clear

reghdfe ln_export_value tr2 $ctrl2, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1B_3_4.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 $ctrl2, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T1B_3_4.xls", append dec(3) adjr2  e(r2_within)


*** T2
use "Sample_ctry_year.dta",clear

reghdfe ln_tr2_valx p_CHN_reserves p_CHN reserves , abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T2.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export ln_tr2_valx_pred,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T2.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export ln_tr2_valx_pred $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T2.xls", append adjr2  e(r2_within)
reghdfe ln_export ln_tr2_valx ln_tr2_valx_r ,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T2.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export ln_tr2_valx ln_tr2_valx_r $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T2.xls", append dec(3) adjr2  e(r2_within)

*** T3
use "Sample_ctry_ind_year.dta",clear
reghdfe ln_export tr2 tr2_rca3_cn RCA_sitc3_china $ctrl0,  abs(ctry_id sitc3 year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T3.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2 tr2_rca3_cn RCA_sitc3_china $ctrl0,  abs(ctry_id ind_yr) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T3.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2 tr2_rca2_cn RCA_sitc2_china $ctrl0,  abs(ctry_id sitc3 year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T3.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2 tr2_rca2_cn RCA_sitc2_china $ctrl0,  abs(ctry_id ind_yr) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T3.xls", append dec(3) adjr2  e(r2_within)

*** T4
use "Sample_firm_ctry_year.dta",clear

reghdfe ln_export_value tr2 tr2_soex, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T4.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex $ctrl1, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T4.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex $ctrl2, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T4.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex $ctrl2, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T4.xls", append dec(3) adjr2  e(r2_within)

*** T5
use "Sample_firm_ctry_year.dta",clear

reghdfe ln_export_value tr2 tr2_soex tr2_soex_ROA soex_ROA tr2_ROA ROA_lag, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T5.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_ROA soex_ROA tr2_ROA ROA_lag LNTA LEV , abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T5.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_ROA soex_ROA tr2_ROA ROA_lag LNTA LEV GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T5.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_ROA soex_ROA tr2_ROA ROA_lag LNTA LEV GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T5.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_export_value tr2 tr2_soex tr2_soex_ROE soex_ROE tr2_ROE ROE_lag, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T5.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_ROE soex_ROE tr2_ROE ROE_lag LNTA LEV , abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T5.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_ROE soex_ROE tr2_ROE ROE_lag LNTA LEV GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T5.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_ROE soex_ROE tr2_ROE ROE_lag LNTA LEV GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T5.xls", append dec(3) adjr2  e(r2_within)

*** T6
use "Sample_firm_ctry_year.dta",clear

reghdfe ln_export_value tr2 tr2_soex tr2_soex_lnemp soex_lnemp tr2_lnemp lnemp_lag, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T6.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_lnemp soex_lnemp tr2_lnemp lnemp_lag LNTA LEV ROA  , abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T6.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_lnemp soex_lnemp tr2_lnemp lnemp_lag LNTA LEV ROA GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T6.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_lnemp soex_lnemp tr2_lnemp lnemp_lag LNTA LEV ROA GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T6.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_export_value tr2 tr2_soex tr2_soex_lnempat soex_lnempat tr2_lnempat lnempat_lag, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T6.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_lnempat soex_lnempat tr2_lnempat lnempat_lag LNTA LEV ROA  , abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T6.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_lnempat soex_lnempat tr2_lnempat lnempat_lag LNTA LEV ROA GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T6.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_lnempat soex_lnempat tr2_lnempat lnempat_lag LNTA LEV ROA GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T6.xls", append dec(3) adjr2  e(r2_within)

*** T7
use "Sample_price.dta",clear

reghdfe ln_price tr2 tr2_soex , abs(id_cs ctry_id hs year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T7.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_price tr2 tr2_soex $ctrl2, abs(id_cs ctry_id hs year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T7.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_price tr2 tr2_soex $ctrl2, abs(id_cs ctry_id hs#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T7.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_price tr2 tr2_soex $ctrl2, abs(id_cs ctry_id#hs#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\T7.xls", append dec(3) adjr2  e(r2_within)

*** F1
** F1A
use "Sample_ctry_year.dta",clear
g _tintra=year-tr2_year

replace _tintra = -4 if _tintra <=-4
replace _tintra =  4 if _tintra >= 4
replace _tintra =. if _tintra==.

tab _tintra, missing 
generate dx_4  = 0
replace dx_4  = 1 if _tintra == -4
generate dx_3  = 0
replace dx_3  = 1 if _tintra == -3
generate dx_2  = 0
replace dx_2  = 1 if _tintra == -2
generate dx_1  = 0
replace dx_1  = 1 if _tintra == -1

generate dx0  = 0
replace dx0 = 1 if _tintra == 0

generate dx1  = 0
replace dx1  = 1 if _tintra == 1
generate dx2  = 0
replace dx2  = 1 if _tintra == 2
generate dx3  = 0
replace dx3  = 1 if _tintra == 3
generate dx4  = 0
replace dx4  = 1 if _tintra == 4
 
g d_4=dx_4
g d_3=dx_3
g d_2=dx_2
g d_1=dx_1
g d1=dx1
g d2=dx2
g d3=dx3
g d4=dx4

label var d_4 "-4"
label var d_3 "-3"
label var d_2 "-2"
label var d_1 "-1"
label var d1 "1"
label var d2 "2"
label var d3 "3"
label var d4 "4"

reghdfe ln_export d_4 d_3 d_2 d_1 d1 d2 d3 d4 GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
coefplot , vertical keep (d_4 d_3 d_2 d_1 d1 d2 d3 d4) yline(0)  title("Ln(Export Value)")

graph save "$repfolder\result\F1A.gph", replace
graph export "$repfolder\result\F1A.png",replace

** F1B. New DiD method (Sun and Abraham 2020)

use "Sample_ctry_year.dta",clear

g _tintra=year-tr2_year

replace _tintra = -4 if _tintra <=-4
replace _tintra =  4 if _tintra >= 4
replace _tintra =. if _tintra==.

tab _tintra, missing 
generate dx_4  = 0
replace dx_4  = 1 if _tintra == -4
generate dx_3  = 0
replace dx_3  = 1 if _tintra == -3
generate dx_2  = 0
replace dx_2  = 1 if _tintra == -2
generate dx_1  = 0
replace dx_1  = 1 if _tintra == -1

generate dx0  = 0
replace dx0 = 1 if _tintra == 0

generate dx1  = 0
replace dx1  = 1 if _tintra == 1
generate dx2  = 0
replace dx2  = 1 if _tintra == 2
generate dx3  = 0
replace dx3  = 1 if _tintra == 3
generate dx4  = 0
replace dx4  = 1 if _tintra == 4
 
g d_4=dx_4
g d_3=dx_3
g d_2=dx_2
g d_1=dx_1
g d1=dx1
g d2=dx2
g d3=dx3
g d4=dx4

label var d_4 "-4"
label var d_3 "-3"
label var d_2 "-2"
label var d_1 "-1"
label var d1 "1"
label var d2 "2"
label var d3 "3"
label var d4 "4"

g never_treat=(mi(tr2_year))
egen maxyear=max(tr2_year)
g last=(year==maxyear)
replace last=1 if never_treat==1
eventstudyinteract ln_export d_4 d_3 d_2 d_1 d1 d2 d3 d4, covariates(GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF) cohort(tr2_year) control_cohort(never_treat) absorb(ctry_id year) vce(cluster ctry_id) 

matrix C = e(b_iw)
mata st_matrix("A",sqrt(diagonal(st_matrix("e(V_iw)"))))
matrix C = C \ A'
matrix list C
coefplot matrix(C[1]), vertical se(C[2]) yline(0)  title("ln(Export Value), SA Method")
graph save "$repfolder\result\F1B.gph", replace
graph export "$repfolder\result\F1B.png",replace

*** F2
use "Sample_firm_ctry_year.dta",clear

reghdfe ln_export_value tr_indx13-tr_indx15 tr_indx17-tr_indx43 $ctrl2, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
coefplot , keep (tr_indx13 tr_indx14 tr_indx15 tr_indx17 tr_indx18 tr_indx19 tr_indx20 tr_indx21  tr_indx22 tr_indx23 tr_indx24 tr_indx25 tr_indx26 tr_indx27 tr_indx28 tr_indx29 tr_indx30 tr_indx31 tr_indx32 tr_indx33 tr_indx34 tr_indx35 tr_indx36 tr_indx37 tr_indx38 tr_indx39 tr_indx40 tr_indx41 tr_indx42 tr_indx43) xline(0)  title("") label
graph save "$repfolder\result\F2.gph", replace
graph export "$repfolder\result\F2.png",replace

log close

*=============================================================================== 
* 2. Tables and Figures in the Online Appendix
*=============================================================================== 

* log file for online appendix
log using "$repfolder\result\log_file_online.log"

*** TA1
** TA1A
use "Sample_ctry_year.dta",clear
codebook ctry_id // 147
eststo clear
estpost sum ln_trade ln_export ln_import tr2 GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF,detail
esttab using "$repfolder\result\TA1A.csv", replace label cells("count mean(fmt(3))  p50(fmt(3)) sd(fmt(3))" ) noobs

** TA1B
use "Sample_ctry_ind_year.dta",clear
eststo clear
estpost sum ln_export tr2 RCA_sitc3_china RCA_sitc2_china,detail
esttab using "$repfolder\result\TA1B.csv", replace label cells("count mean(fmt(3))  p50(fmt(3)) sd(fmt(3))" ) noobs

** TA1C
use "Sample_firm_ctry_year.dta",clear
eststo clear
estpost sum ln_export_value tr2 soex LNTA LEV ROA ROE_lag lnemp_lag lnempat_lag,detail
esttab using "$repfolder\result\TA1C.csv", replace label cells("count mean(fmt(3))  p50(fmt(3)) sd(fmt(3))" ) noobs

** TA1D
use "Sample_price.dta",clear
eststo clear
estpost sum ln_price tr2 soex,detail
esttab using "$repfolder\result\TA1D.csv", replace label cells("count mean(fmt(3))  p50(fmt(3)) sd(fmt(3))" ) noobs

*** TA2
use "Sample_ctry_year.dta",clear

reghdfe ln_export tr2_Grant tr2_Loan tr2_Tech tr2_Other,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA2.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2_Grant tr2_Loan tr2_Tech tr2_Other $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA2.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_export tr2_ODA tr2_OTHER,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA2.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2_ODA tr2_OTHER $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA2.xls", append dec(3) adjr2  e(r2_within)

*** TA3
use "Sample_ctry_year.dta",clear
* generalized DiD
reghdfe ln_export ln_tr2_val_post,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA3.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export ln_tr2_val_post $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA3.xls", append dec(3) adjr2  e(r2_within)

* lagged aid value
reghdfe ln_export ln_tr2_valx,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA3.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export ln_tr2_valx $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA3.xls", append dec(3) adjr2  e(r2_within)

* Synthetic Difference in Differences (Arkhangelsky et al 2021)
* Dmitry Arkhangelsky, Susan Athey, David A. Hirshberg, Guido W. Imbens, and Stefan Wager. Synthetic Difference in Differences, American Economic Review, December 2021.
use "Sample_ctry_year.dta",clear
* create balanced panel
bysort ctry_id: g yrN=_N
sum yrN
keep if yrN==15
count // 1,500

sdid ln_export ctry_id year tr2, vce(bootstrap) seed(1213)
/*
-----------------------------------------------------------------------------
   ln_export |     ATT     Std. Err.     t      P>|t|    [95% Conf. Interval]
-------------+---------------------------------------------------------------
   treatment |   0.41667    0.10885     3.83    0.000     0.20333     0.63001
-----------------------------------------------------------------------------
*/
sdid ln_export ctry_id year tr2, vce(bootstrap) seed(1213) covariates(GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF)
/*
-----------------------------------------------------------------------------
   ln_export |     ATT     Std. Err.     t      P>|t|    [95% Conf. Interval]
-------------+---------------------------------------------------------------
   treatment |   0.43518    0.11299     3.85    0.000     0.21373     0.65662
-----------------------------------------------------------------------------
*/

*** TA4
use "Sample_ctry_year.dta",clear
* exclude all countries with first aid year is 2000, 2001, 2002
reghdfe ln_export tr2 $ctrl0 if tr2_year~=2000 & tr2_year~=2001 & tr2_year~=2002,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA4.xls", replace dec(3) adjr2  e(r2_within)
* Aid with value in the top 25% percentile
reghdfe ln_export tr2_top25 $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA4.xls", append dec(3) adjr2  e(r2_within)
* income-cutoff by year FE
reghdfe ln_export tr2 $ctrl0,  abs(ctry_id income_yr) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA4.xls", append dec(3) adjr2  e(r2_within)
* country linear trends
reghdfe ln_export tr2 $ctrl0,  abs(ctry_id#c.year ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA4.xls", append dec(3) adjr2  e(r2_within)
replace export=0 if mi(export)
g export_IHS=log(export + sqrt(export^2 + 1))
reghdfe export_IHS tr2 $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA4.xls", append dec(3) adjr2  e(r2_within)

*** TA5
use "Sample_ctry_year.dta",clear
g tr2_f3yr=(tr2_year~=. & (year-tr2_year)>=1 & (year-tr2_year)<=4)
drop if tr2_year~=. & (year-tr2_year)>3

reghdfe ln_trade tr2_f3yr ,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA5.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_trade tr2_f3yr $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA5.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_export tr2_f3yr,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA5.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2_f3yr $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA5.xls", append dec(3) adjr2  e(r2_within)

*** TA6
** TA6A
use "Sample_ctry_year.dta",clear
drop if Income_Indicator_2000=="H"
reghdfe ln_trade tr2 ,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6A.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_trade tr2 $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6A.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_export tr2,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6A.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2 $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6A.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_import tr2,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6A.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_import tr2 $ctrl0,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6A.xls", append dec(3) adjr2  e(r2_within)

** TA6B_1_2
use "Sample_ctry_ind_year.dta",clear
drop if Income_Indicator_2000=="H"
reghdfe ln_export tr2 $ctrl0,  abs(ctry_id sitc3 year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6B_1_2.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2 $ctrl0,  abs(ctry_id ind_yr) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6B_1_2.xls", append dec(3) adjr2  e(r2_within)

** TA6B_3_4
use "Sample_firm_ctry_year.dta",clear
drop if Income_Indicator_2000=="H"
reghdfe ln_export_value tr2 $ctrl1, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6B_3_4.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 $ctrl2, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA6B_3_4.xls", append  dec(3) adjr2  e(r2_within)

*** TA7
use "Sample_ctry_ind_year.dta",clear
drop if Income_Indicator_2000=="H"
reghdfe ln_export tr2 tr2_rca3_cn RCA_sitc3_china $ctrl0,  abs(ctry_id sitc3 year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA7.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2 tr2_rca3_cn RCA_sitc3_china $ctrl0,  abs(ctry_id ind_yr) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA7.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_export tr2 tr2_rca2_cn RCA_sitc2_china $ctrl0,  abs(ctry_id sitc3 year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA7.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export tr2 tr2_rca2_cn RCA_sitc2_china $ctrl0,  abs(ctry_id ind_yr) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA7.xls", append dec(3) adjr2  e(r2_within)

*** TA8
use "Sample_ctry_ind_year_US.dta",clear
reghdfe ln_export ln_us_aid_val $ctrl0,  abs(ctry_id sitc3 year) cluster(ctry_id) keepsing nowarn 
outreg2 using "$repfolder\result\TA8.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export ln_us_aid_val $ctrl0,  abs(ctry_id sitc3_yr) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA8.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export ln_us_aid_val ln_us_aid_val_rca3_usa RCA_sitc3_usa $ctrl0,   abs(ctry_id sitc3 year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA8.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export ln_us_aid_val ln_us_aid_val_rca3_usa $ctrl0,  abs(ctry_id sitc3_yr) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA8.xls", append dec(3) adjr2  e(r2_within)

*** TA9
use "Sample_firm_ctry_year.dta",clear
reghdfe ln_export_value ln_tr2_valx_pred ln_tr2_valx_pred_soex, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA9.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export_value ln_tr2_valx_pred ln_tr2_valx_pred_soex $ctrl1, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA9.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value ln_tr2_valx_pred ln_tr2_valx_pred_soex $ctrl2, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA9.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value ln_tr2_valx_pred ln_tr2_valx_pred_soex $ctrl2, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA9.xls", append dec(3) adjr2  e(r2_within)

*** TA10
use "Sample_firm_ctry_year.dta",clear
reghdfe ln_export_value tr2 tr2_soex $ctrl2, abs(id_cs ctry_id ind2#year provid#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA10.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export_value ln_tr2_valx_pred ln_tr2_valx_pred_soex $ctrl2, abs(id_cs ctry_id ind2#year provid#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA10.xls", append dec(3) adjr2  e(r2_within)

reghdfe ln_export_value tr2 tr2_soex $ctrl2 if year_num==14, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA10.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value ln_tr2_valx_pred ln_tr2_valx_pred_soex $ctrl2 if year_num==14, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA10.xls", append dec(3) adjr2  e(r2_within)

*** TA11
use "Sample_firm_ctry_year.dta",clear
drop if Income_Indicator_2000=="H"
reghdfe ln_export_value tr2 tr2_soex, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA11.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex $ctrl1, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA11.xls", append  dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex $ctrl2, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA11.xls", append  dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex $ctrl2, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA11.xls", append  dec(3) adjr2  e(r2_within)

*** TA12
use "Sample_firm_ctry_year.dta",clear
* sales growth
reghdfe ln_export_value tr2 tr2_soex tr2_soex_gsale soex_gsale tr2_gsale gsale_lag, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA12.xls", replace dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_gsale soex_gsale tr2_gsale gsale_lag LNTA LEV ROA , abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA12.xls", append  dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_gsale soex_gsale tr2_gsale gsale_lag LNTA LEV ROA GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA12.xls", append dec(3) adjr2  e(r2_within)
reghdfe ln_export_value tr2 tr2_soex tr2_soex_gsale soex_gsale tr2_gsale gsale_lag LNTA LEV ROA GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF, abs(id_cs ctry_id ind2#year) cluster(ctry_id) keepsing nowarn
outreg2 using "$repfolder\result\TA12.xls", append dec(3) adjr2  e(r2_within)

*** FA1
use "Sample_aid_year.dta",clear
*FA1A. number by year
twoway bar Num year, title("Panel A")
graph save "$repfolder\result/FA1A",replace
graph export "$repfolder\result/FA1A.png",replace

*FA1B. value by year
twoway bar Val_sum year, title("Panel B")
graph save "$repfolder\result/FA1B",replace
graph export "$repfolder\result/FA1B.png",replace

*FA1C. percentage of countries receiving aid from China
use "Sample_ctry_year.dta",clear
bysort year: egen tr2_mean=mean(tr2)
bysort year: keep if _n==1
keep year tr2_mean
label var tr2_mean "Percent of Countries receiving Aid from China"
* number by year
twoway bar tr2_mean year, title("Panel C")
graph save "$repfolder\result/FA1C",replace
graph export "$repfolder\result/FA1C.png",replace

*** FA2
** FA2A
use "Sample_ctry_year.dta",clear
drop if Income_Indicator_2000=="H"
g _tintra=year-tr2_year

replace _tintra = -4 if _tintra <=-4
replace _tintra =  4 if _tintra >= 4
replace _tintra =. if _tintra==.

tab _tintra, missing 
generate dx_4  = 0
replace dx_4  = 1 if _tintra == -4
generate dx_3  = 0
replace dx_3  = 1 if _tintra == -3
generate dx_2  = 0
replace dx_2  = 1 if _tintra == -2
generate dx_1  = 0
replace dx_1  = 1 if _tintra == -1

generate dx0  = 0
replace dx0 = 1 if _tintra == 0

generate dx1  = 0
replace dx1  = 1 if _tintra == 1
generate dx2  = 0
replace dx2  = 1 if _tintra == 2
generate dx3  = 0
replace dx3  = 1 if _tintra == 3
generate dx4  = 0
replace dx4  = 1 if _tintra == 4
 
g d_4=dx_4
g d_3=dx_3
g d_2=dx_2
g d_1=dx_1
g d1=dx1
g d2=dx2
g d3=dx3
g d4=dx4

label var d_4 "-4"
label var d_3 "-3"
label var d_2 "-2"
label var d_1 "-1"
label var d1 "1"
label var d2 "2"
label var d3 "3"
label var d4 "4"

reghdfe ln_export d_4 d_3 d_2 d_1 d1 d2 d3 d4 GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF,  abs(ctry_id year) cluster(ctry_id) keepsing nowarn
coefplot , vertical keep (d_4 d_3 d_2 d_1 d1 d2 d3 d4) yline(0)  title("Ln(Export Value)")

graph save "$repfolder\result\FA2A.gph", replace
graph export "$repfolder\result\FA2A.png",replace

** FA2B. New DiD method (Sun and Abraham 2020)

use "Sample_ctry_year.dta",clear
drop if Income_Indicator_2000=="H"

g _tintra=year-tr2_year

replace _tintra = -4 if _tintra <=-4
replace _tintra =  4 if _tintra >= 4
replace _tintra =. if _tintra==.

tab _tintra, missing 
generate dx_4  = 0
replace dx_4  = 1 if _tintra == -4
generate dx_3  = 0
replace dx_3  = 1 if _tintra == -3
generate dx_2  = 0
replace dx_2  = 1 if _tintra == -2
generate dx_1  = 0
replace dx_1  = 1 if _tintra == -1

generate dx0  = 0
replace dx0 = 1 if _tintra == 0

generate dx1  = 0
replace dx1  = 1 if _tintra == 1
generate dx2  = 0
replace dx2  = 1 if _tintra == 2
generate dx3  = 0
replace dx3  = 1 if _tintra == 3
generate dx4  = 0
replace dx4  = 1 if _tintra == 4
 
g d_4=dx_4
g d_3=dx_3
g d_2=dx_2
g d_1=dx_1
g d1=dx1
g d2=dx2
g d3=dx3
g d4=dx4

label var d_4 "-4"
label var d_3 "-3"
label var d_2 "-2"
label var d_1 "-1"
label var d1 "1"
label var d2 "2"
label var d3 "3"
label var d4 "4"

g never_treat=(mi(tr2_year))
egen maxyear=max(tr2_year)
g last=(year==maxyear)
replace last=1 if never_treat==1
eventstudyinteract ln_export d_4 d_3 d_2 d_1 d1 d2 d3 d4, covariates(GDP_g ln_GDP_pc EXP_ratio IMP_ratio polity2 FDI_NIF) cohort(tr2_year) control_cohort(never_treat) absorb(ctry_id year) vce(cluster ctry_id) 

matrix C = e(b_iw)
mata st_matrix("A",sqrt(diagonal(st_matrix("e(V_iw)"))))
matrix C = C \ A'
matrix list C
coefplot matrix(C[1]), vertical se(C[2]) yline(0)  title("ln(Export Value), SA Method")
graph save "$repfolder\result\FA2B.gph", replace
graph export "$repfolder\result\FA2B.png",replace

log close